//
//  CMXMLParser.m
//  CocoaMesh
//
//  Created by George  Armah on 4/12/09.
//  Copyright 2009 Lafayette College. All rights reserved.
//

#import "CMXMLParser.h"


@implementation CMXMLParser


-(id) init 
{
	if (self = [super init]) 
	{
		parsingElement = [[NSMutableString alloc] initWithCapacity:20];
		authToken = [[NSMutableString alloc] initWithCapacity:50];
		[authToken setString:@""];
		
		//This should default to NO ... setting it here to make sure
		parsingAuthToken = NO;
	}
	return self;
}


-(NSString *)parseXML:(NSData*)xml 
   forElementContents:(NSString*)elementName 
		   parseError:(NSError **)err
{
	[parsingElement setString:elementName];
	
	NSXMLParser * parser = [[NSXMLParser alloc] initWithData:xml];
	[parser setDelegate:self];
	[parser parse];
	
	if (err && [parser parserError])
	{
		*err = [parser parserError];
	}
	
	[parser release];
	
	return [authToken copy];
}


- (void)  parser:(NSXMLParser *)par 
 didStartElement:(NSString *)elementName 
	namespaceURI:(NSString *)namespaceURI 
   qualifiedName:(NSString *)qualifiedName 
	  attributes:(NSDictionary *)attributeDict 
{
	if ([elementName isEqualToString:parsingElement]) 
	{
		parsingAuthToken = YES;
		//NSLog(@"Found %@", elementName);
	}
	
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{
    if (parsingAuthToken) 
	{
		[authToken appendString:string];
    }
}


- (void)  parser:(NSXMLParser *)parser 
   didEndElement:(NSString *)elementName 
	namespaceURI:(NSString *)namespaceURI 
   qualifiedName:(NSString *)qName 
{
	if([elementName isEqualToString:parsingElement]) 
	{
		//NSLog(@"Finished Parsing authToken %@", authToken);
		parsingAuthToken = !parsingAuthToken;
	}
	
}



-(void) dealloc 
{
	[parsingElement release];
	[authToken release];
	[super dealloc];
}

@end
